package com.example.xiongvideocontrol.view;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;


import androidx.annotation.Nullable;

import com.example.xiongvideocontrol.util.VideoGestureControlUtil;

/**
 * Author: xiongyeping
 * Date: 2021/1/16 16:26
 * Description:一个视频控件
 */

public class MyVideoView extends View {


    private VideoGestureControlUtil videoGestureControlUtil;
    private double playIndex = 0;//视频当前播放位置


    public MyVideoView(Context context) {
        this(context, null);
    }

    public MyVideoView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyVideoView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        videoGestureControlUtil = new VideoGestureControlUtil(context, this);
        videoGestureControlUtil.setOnVideoControlListener(new VideoGestureControlUtil.OnVideoControlListener() {
            @Override
            public double getScrollVideoLenth() {
                return 36 * 60 * 1000;//36分钟的视频
            }

            @Override
            public double getScrollVideoPlayingIndex() {
                return playIndex;
            }

            @Override
            public void setScrollVideoPlayingIndex(double playIndex) {
                MyVideoView.this.playIndex = playIndex;
            }

            @Override
            public void onDoubleTap() {
                Log.d("print", "onSingleTap: 双击");
            }

            @Override
            public void onSingleTap() {
                Log.d("print", "onSingleTap: 单击");
            }

            @Override
            public Rect getVideoViewRect() {
                Rect rect = new Rect();
                boolean localVisibleRect = getGlobalVisibleRect(rect);
                if (localVisibleRect) {
                    return rect;
                }
                return null;
            }
        });


    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return videoGestureControlUtil.touch(event);
    }
}
